home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’95
/
Search Example
/
Main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-23
|
8KB
|
483 lines
/*Main.c*/
/*
This is a simple skeleton application that opens a window, handles the menu bar,
and dispatches events. See MenuItem for menu dispatches, CkEvents for the event
loop. Not much of interest here....
All the good stuff is in AEVT.c
*/
#include <stdio.h>
#include <string.h>
#include <GestaltEqu.h>
#include <AppleEvents.h>
#include <Types.h>
#include <Memory.h>
#include <Windows.h>
#include <Dialogs.h>
#include <Menus.h>
#include <Fonts.h>
#include <Events.h>
#include <OSEvents.h>
#include <Desk.h>
#include <ToolUtils.h>
#include "main.h"
#include "AEVT.h"
#include "TTY_Messages.h"
#define THEWINID 128
#define DBGWINID 129
#define ERRALRT 129
#define CREATORID 129
#define KEYMASK 0xff
#define MENUCOUNT 3
#define DeskResID 128
#define FileResID 129
#define EditResID 130
#define OptionResID 131
#define Desk_ID 0
#define File_ID 1
#define Edit_ID 2
#define Option_ID 3
/*** Globals ***/
long gOSVersion;
/*** Local to Template.c ***/
static WindowPtr theWin,curWin,dbgWin;
static WindowPtr whichWindow;
static short windowCode, stillGoAway;
static MenuHandle menuList[MENUCOUNT];
static EventRecord theEvent;
static int runningMulti;
static int inForeground;
static FILE *f;
int theWinOpen,done;
static int waitct;
static OSType ftype;
static char s[80];
/***************************/
Click()
{
while (!Button());
while (Button());
}
/***************************/
Error(msg)
char *msg;
{
char mesg[255];
strcpy(mesg,msg);
ParamText(mesg,"","","");
Alert(ERRALRT,0);
}
/*********************/
CheckMulti()
#define WNETrapNum 0x60
#define UnImplTrapNum 0x9F
{
int i;
if (NGetTrapAddress(WNETrapNum, ToolTrap) ==
NGetTrapAddress(UnImplTrapNum, ToolTrap))
runningMulti = FALSE;
else {
runningMulti = TRUE;
for (i=0;i<3;i++)
EventAvail(everyEvent, &theEvent);
}
inForeground = TRUE;
}
/***************************/
Shutdown()
{
int i;
for (i=0;i<3;i++)
EventAvail(everyEvent, &theEvent);
if (theWinOpen)
DisposeWindow(theWin);
}
/***********************************************************************************/
/********************** Mac User Interface stuff below this line *******************/
/***********************************************************************************/
/********* Initialization **********/
Die()
{
ExitToShell();
}
/***************************/
WindowSetup()
{
Rect r;
theWinOpen = 1;
theWin = GetNewWindow(128,NULL,(char *) -1);
if (!theWin)
SysBeep(10);
SetPort(theWin);
r = theWin->portRect;
InsetRect(&r,5,5);
// r.top = r.bottom - 150;
TTY_InitMessages(theWin,r,monaco,12);
}
/***************************/
MenuSetup()
{
int i;
menuList[Desk_ID] = GetMenu(DeskResID);
AddResMenu(menuList[Desk_ID], 'DRVR');
menuList[File_ID] = GetMenu(FileResID);
menuList[Edit_ID] = GetMenu(EditResID);
menuList[Option_ID] = GetMenu(OptionResID);
for (i=0;i<MENUCOUNT;i++)
InsertMenu(menuList[i], 0);
DrawMenuBar();
}
/***************************/
int CheckOS()
{
OSErr err;
char s[256];
gOSVersion = 0;
if (err=Gestalt(gestaltSystemVersion,&gOSVersion)) {
sprintf(s,"Error calling Gestalt %d\r",err);
}
else {
sprintf(s,"O/S=%0lX\r",gOSVersion);
}
TTY_WriteMessage(s);
return err;
}
/***************************/
Initialize()
{
MaxApplZone();
MoreMasters();
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent,0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(Die);
InitCursor();
WindowSetup();
MenuSetup();
CheckMulti();
if (CheckOS())
done=1;
else
done=0;
if (gOSVersion>=0x700) /*System 7 or better*/
InitAppleEvents();
}
/****** Event stuff follows *******/
MDeskAbout()
{
int i;
Handle h;
i=Alert(128,0);
}
MFileQuit()
{
done=1;
}
/*** Menu Dispatcher ***/
MenuItem(menuID,itemNumber)
short menuID, itemNumber;
{
Str255 daName;
GrafPtr theCurrentPort;
char *tempstr,temp[35];
int i;
switch (menuID-1) {
case Desk_ID:
switch (itemNumber) {
case 1:
MDeskAbout();
break;
case 2:
break;
default:
EnableItem(menuList[Edit_ID],0);
GetItem(menuList[Desk_ID],itemNumber,&daName);
GetPort(&theCurrentPort);
OpenDeskAcc(&daName);
SetPort(theCurrentPort);
break;
}
break;
case File_ID:
switch (itemNumber) {
case 1:
MFileQuit();
break;
}
break;
case Edit_ID:
SystemEdit(itemNumber-1);
break;
default:
break;
}/*case*/
HiliteMenu(0);
}/**MenuItem**/
/***** Menu key front end *****/
DoMenu(menuResult)
long menuResult;
{
short menuID, itemNumber;
Str255 daName;
GrafPtr theCurrentPort;
char *tempstr;
menuID = HiWord(menuResult);
itemNumber = LoWord(menuResult);
MenuItem(menuID,itemNumber);
}/*DoMenu*/
/**** Mouse event handler ****/
DoMouseDown()
{
windowCode = FindWindow(theEvent.where,&whichWindow);
switch (windowCode) {
case inMenuBar:
DoMenu(MenuSelect(theEvent.where));
break;
case inSysWindow:
SystemClick(&theEvent,whichWindow);
break;
case inDesk:
break;
case inContent:
SelectWindow(whichWindow);
break;
case inDrag:
DragWindow(whichWindow, theEvent.where, &(screenBits.bounds));
break;
case inGrow:
break;
case inGoAway:
if (whichWindow==theWin)
if (TrackGoAway(whichWindow, theEvent.where))
done = TRUE;
break;
default:
break;
}
}/*DoMouseDone*/
/***** Key event handler *****/
DoKeyDown()
{
short menuID, itemNumber;
long menuResult;
char globCh;
globCh = theEvent.message & KEYMASK;
if (theEvent.modifiers & cmdKey) {
menuResult=MenuKey(globCh);
menuID = HiWord(menuResult);
itemNumber = LoWord(menuResult);
MenuItem(menuID,itemNumber);
HiliteMenu(0);
}/*if*/
}
/**** Update event handler ****/
DoUpdate()
{
GrafPtr curPort;
WindowPtr curWin;
curWin = (WindowPtr) theEvent.message;
GetPort(&curPort);
SetPort(curWin);
BeginUpdate(curWin);
/**/
if (curWin==theWin) {
TTY_RedrawMessages();
}
EndUpdate(curWin);
SetPort(curPort);
}
/**********************************/
DoMulti(myEvent)
EventRecord *myEvent;
{
#define MouseMovedEvt 0xFA
Byte HiByte;
long bit0;
int sysresult;
long MouseMove;
bit0 = 31;
MouseMove = myEvent->message;
HiByte = (Byte)((char *)(&MouseMove)[0]);
if (HiByte == MouseMovedEvt)
;
if (myEvent->message & 1) {
inForeground = TRUE;
}
else {
inForeground = FALSE;
}
}
/****** Event LOOP guts ******/
int CkEvents()
{
#define MultiFinderEvt 15
long sleep;
int doit;
OSErr err;
char s[80];
if (inForeground)
sleep = 0;
else
sleep = 20;
if (runningMulti) {
doit = WaitNextEvent(everyEvent, &theEvent, sleep, NULL);
}
else {
SystemTask(); /*Let desk accs have some time*/
doit = GetNextEvent(everyEvent, &theEvent); /*check for pending events*/
}
if (doit) {
switch (theEvent.what) {
case kHighLevelEvent:
err = AEProcessAppleEvent( &theEvent ) ;
if (err!=noErr) {
sprintf(s, "Error handling HighLevelEvent %d\r", err);
TTY_WriteMessage(s);
}
break;
case mouseDown:
DoMouseDown();
break;
case mouseUp:
break;
case autoKey:
case keyDown:
DoKeyDown();
break;
case keyUp:
break;
case activateEvt:
break;
case updateEvt:
DoUpdate();
break;
case diskEvt:
break;
case networkEvt:
break;
case driverEvt:
break;
case MultiFinderEvt:
DoMulti(&theEvent);
break;
case nullEvent:
break;
default:
break;
}/*switch the...*/
return (theEvent.what);
}/*if getnext...*/
else return (nullEvent);
}
/*****************************************/
main()
{
Initialize();
while (!done)
CkEvents();
Shutdown();
ExitToShell();
}